/*
 * =====================================================================================
 *
 *       Filename:  tcpConnect.h
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  07/13/2012 03:01:07 PM
 *       Revision:  none
 *       Compiler:  gcc/g++
 *
 *         Author:  wangtengchao , wangtengchao AT gmail.com
 *   Organization:
 *
 * =====================================================================================
 */
#ifndef TCPCONNECT_H
#define TCPCONNECT_H
#include"netBuffer.h"//for class NetBuffer
#include"inetAddr.h"//for class InetAddr
#include"eventHandler.h" //for base class EventHandler
#include"socket.h"//for socket
#include "timestamp.h"
#include<string>
namespace inm
{

	namespace net
	{
        class Dispatcher;
        class TcpConnect : public EventHandler,public boost::enable_shared_from_this<TcpConnect>
		{
			public:
                TcpConnect(const std::string& name,
                           Dispatcher* dispatcher,
                           int sockfd,
                           const InetAddr& localAddr,
                           const InetAddr& peerAddr
                           );
                ~TcpConnect();
                void handleRead();
                void handleWrite();
                void handleClose();

                void sendMessage(const NetBuffer& buf)
                {
                    sendMessage(buf.toString());
                }
				void sendMessage(const std::string& msg)
				{
					sendMessage(msg.c_str(),msg.length());
				}
                //the true sendMessage
                void sendMessage(const char* msg,size_t num);

                void setMessageCallback(const MessageCallback& cb)
                {
                    messageCallback_ = cb;
                }


                void setConnectCallback(const ConnectionCallback& cb)
                {
                    connectCallback_ = cb;
                }
                void setWriteCallback(const WriteCompleteCallback& cb)
                {
                    writeComleteCallback_ = cb;
                }
               int getSocketFdHandle() const {return socket_.getSockFd();}
				void setNoDelay(bool b);
               void connectEstablished();
               void writeComplete();
            private:

				InetAddr localAddr_;
				InetAddr peerAddr_;
                Socket socket_;
				NetBuffer inputBuffer_;
                NetBuffer outputBuffer_;
                //from tcp server set--user costumed
                MessageCallback messageCallback_;
                ConnectionCallback connectCallback_;
                WriteCompleteCallback writeComleteCallback_;
                //have more to write in tasks ,set true
                //after task over,then set false
                bool hasMoreToWrite;//outputBuffer_ is not empty

		};

	};
};
#endif
